#include <bits/stdc++.h>
using namespace std;

int main() 
{
    string T;
    cin >> T;

    int N;
    cin >> N;

    vector< vector<string> > bags(N);
    for (int i = 0; i < N; ++i) 
	{
        int Ai;
        cin >> Ai;
        for (int j = 0; j < Ai; ++j) 
		{
            string Sij;
            cin >> Sij;
            bags[i].push_back(Sij);
        }
    }

    vector<int> dp(T.size() + 1, INT_MAX);
    dp[0] = 0; // Base case

    for (auto& bag : bags) 
	{
        vector<int> new_dp = dp; // Copy the current state of dp
        for (auto& str : bag) 
		{
            for (int j = 0; j <= T.size(); j++) 
			{
                if (dp[j] != INT_MAX && j + str.size() <= T.size() && T.substr(j, str.size()) == str) 
				{
                    new_dp[j + str.size()] = min(new_dp[j + str.size()], dp[j] + 1);
                }
            }
        }
        dp = new_dp; // Update the dp array for the next iteration
    }

    if (dp[T.size()] == INT_MAX) 
	{
        cout << "-1\n"; // Not possible to form T
    } 
	else 
	{
        cout << dp[T.size()] << "\n"; // Minimum cost to form T
    }
    return 0;
}
